﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Document.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the Document type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Common
{
    using System.ComponentModel;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// Defines the <seealso cref="Document"/> type.
    /// </summary>
    public class Document : INotifyPropertyChanged
    {
        /// <summary>
        /// Title of document
        /// </summary>
        private string _title;

        /// <summary>
        /// Initializes a new instance of the <see cref="Document"/> class.
        /// </summary>
        public Document()
            : this("Untitled")
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Document"/> class.
        /// </summary>
        /// <param name="title">The title.</param>
        public Document(string title)
        {
            Contract.Requires(string.IsNullOrEmpty(title) == false, "A document title should not be null or empty");
            _title = title;
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets or sets the document title.
        /// </summary>
        /// <value>
        /// The document title.
        /// </value>
        public string DocumentTitle
        {
            get
            {
                return _title;
            }

            set
            {
                _title = value;
                RaisePropertyChanged("DocumentTitle");
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance is active.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is active; otherwise, <c>false</c>.
        /// </value>
        public bool IsActive { get; private set; }

        /// <summary>
        /// Gets or sets the name of the context menu group.
        /// </summary>
        /// <value>
        /// The name of the context menu group.
        /// </value>
        public string ContextMenuGroupName { get; set; }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>
        /// The view for document
        /// </value>
        public IViewBase View { get; set; }

        #region Document lifecycle event methods

        /// <summary>
        /// Activateds this instance.
        /// </summary>
        public virtual void Activated()
        {
            IsActive = true;
        }

        /// <summary>
        /// Losts the activation.
        /// </summary>
        public virtual void LostActivation()
        {
            IsActive = false;
        }

        /// <summary>
        /// Documents the closed.
        /// </summary>
        public virtual void DocumentClosed()
        {
        }

        #endregion Document lifecycle event methods

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raises the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            Contract.Requires(string.IsNullOrWhiteSpace(propertyName) == false, "The property name must not be null or empty");

            var handler = PropertyChanged;
            if (handler == null)
            {
                return;
            }

            var args = new PropertyChangedEventArgs(propertyName);
            handler(this, args);
        }

        #endregion INotifyPropertyChanged Members
    }
}